//
//  PhotoViewController.m
//  LPSNews
//
//  Created by Ruan on 14-10-24.
//  Copyright (c) 2014年 LPS. All rights reserved.
//

#import "PicturePhotoViewController.h"
#import "AFNetworking.h"
#import "SDWebImageManager.h"
#import "PhotoMode.h"


#define kPhotoURLString @"http://c.3g.163.com/photo/api/set/0096/"

@interface PicturePhotoViewController () <UIScrollViewDelegate>

@property (nonatomic, retain) NSMutableArray * imageurlArray;
@property (nonatomic, retain) NSMutableArray * noteArray;
@property (nonatomic, retain) AFHTTPRequestOperationManager *requestManager;
@property (nonatomic, retain) SDWebImageManager *manager ;
@property (nonatomic, assign) BOOL isHidden;
@property (nonatomic, retain) UIScrollView *photoScrollView;
@property (nonatomic, retain) UILabel * photoNumLable;
@property (nonatomic, retain) UITextView *photoTextView;
@property (nonatomic, retain) UILabel *titleLable ;
@property (nonatomic, retain) NSArray *imagetitleArray;
@property (nonatomic, retain) UIImageView *imageView;
@end



@implementation PicturePhotoViewController


- (void)dealloc
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [self.manager cancelAll];
    [self.requestManager.operationQueue cancelAllOperations];
    
    [_photoScrollView release];
    [_imageurlArray release];
    [_noteArray release];
    [_photoNumLable release];
    [_photoTextView release];
    
    [super dealloc];
    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.backgroundColor = [UIColor blackColor];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
 // "photosetID": "4GJ60096|49889”,
    
    UIScrollView *photoScrollView = [[UIScrollView alloc] init];
    
    photoScrollView.showsVerticalScrollIndicator = NO;
    photoScrollView.pagingEnabled = YES;
    photoScrollView.backgroundColor = [UIColor blackColor];
    photoScrollView.delegate = self;
    self.photoScrollView = photoScrollView;
    
    
    UILabel *titleLable = [[UILabel alloc] init];
    titleLable.frame = CGRectMake(15, [UIScreen mainScreen].bounds.size.height*0.60, [UIScreen mainScreen].bounds.size.width * 0.7 , 50);
    titleLable.textColor = [UIColor whiteColor];
    titleLable.font = [UIFont systemFontOfSize:17];
    self.titleLable = titleLable;
    
    
    UILabel *photoNumLable = [[UILabel alloc] init];
    
    photoNumLable.frame = CGRectMake([UIScreen mainScreen].bounds.size.width *0.85, [UIScreen mainScreen].bounds.size.height*0.60, [UIScreen mainScreen].bounds.size.width * 0.2 , 50);
    photoNumLable.textColor = [UIColor whiteColor];
    photoNumLable.font = [UIFont systemFontOfSize:15];
    self.photoNumLable = photoNumLable;
    
    
    UITextView *photoTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.height*0.66, [UIScreen mainScreen].bounds.size.width -30, [UIScreen mainScreen].bounds.size.height*0.20)];
    
    photoTextView.userInteractionEnabled = NO;
    photoTextView.textColor = [UIColor lightGrayColor];
    photoTextView.font = [UIFont systemFontOfSize:13];
    photoTextView.backgroundColor = [UIColor clearColor];
    
    self.photoTextView = photoTextView;

    [self.view addSubview:photoScrollView];
    [self.view addSubview:titleLable];
    [self.view addSubview:photoNumLable];
    [self.view addSubview:photoTextView];

    UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureClick:)] autorelease];
    
    [self.view addGestureRecognizer:tapGesture];
    
    [photoNumLable release];
    [photoScrollView release];
    [titleLable release];
    [photoTextView release];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
   

    NSString *photoDetail = [NSString stringWithFormat:@"%@/%@.json",kPhotoURLString,self.setid];
    
    AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];
    [requestManager GET:photoDetail parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *imageurlArray = [[NSMutableArray alloc] init];
        NSMutableArray *noteArray = [[NSMutableArray alloc] init];
        NSMutableArray *imagetitleArray = [[NSMutableArray alloc] init];
        
        NSDictionary *dict = (NSDictionary *)responseObject;
        
        PhotoMode *phtoData = [[PhotoMode alloc] init];
        [phtoData setValuesForKeysWithDictionary:dict];
        
        self.titleLable.text = phtoData.setname;
       
        for (NSDictionary *dict in phtoData.photos) {
            
            NSString *imageurlStr = [dict objectForKey:@"imgurl"];
            [imageurlArray addObject:imageurlStr];
            NSString *noteStr = [dict objectForKey:@"note"];
            [noteArray addObject:noteStr];
            NSString *imgtitle = [dict objectForKey:@"imgtitle"];
            [imagetitleArray addObject:imgtitle];
            
        }
  
        
        self.imageurlArray = imageurlArray;
        self.noteArray = noteArray;
        self.imagetitleArray = imagetitleArray;
        [phtoData release];

        self.photoScrollView.contentSize = CGSizeMake((imageurlArray.count) * [UIScreen mainScreen].bounds.size.width, 0);

        SDWebImageManager *manager  = [SDWebImageManager sharedManager];
        self.manager = manager;
        
            [manager downloadImageWithURL:self.imageurlArray[0] options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                
            } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                
                self.photoScrollView.frame = CGRectMake(0,-64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
                
                UIImageView *imageView = [[UIImageView alloc] init];
                
                imageView.image = image;
                self.imageView = imageView;
                if (image.size.width > [UIScreen mainScreen].bounds.size.width) {
                    imageView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height *0.15, [UIScreen mainScreen].bounds.size.width, image.size.height * [UIScreen mainScreen].bounds.size.width /  image.size.width);
                }else
                {
                    imageView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height *0.15, [UIScreen mainScreen].bounds.size.width, image.size.height * [UIScreen mainScreen].bounds.size.width /  image.size.width);
                    
                }
                
                [self.photoScrollView addSubview:imageView];
                
                if([noteArray[0] isEqualToString:@""])
                {
                    self.photoTextView.text = imagetitleArray[0];
                    
                }else
                {
                    self.photoTextView.text = noteArray[0];
                }
                
           
                self.photoNumLable.text = [NSString stringWithFormat:@"1/%lu",(unsigned long)self.noteArray.count ];
                
                [imageView release];
                
            }];
            
        
        [imageurlArray release];
        [noteArray release];
        [imagetitleArray release];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];

}

- (void)tapGestureClick:(UITapGestureRecognizer *)tap
{
    
    self.navigationController.navigationBar.alpha = _isHidden;
    
    _isHidden = !_isHidden;
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    int index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
   
        [self.manager downloadImageWithURL:self.imageurlArray[index] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            
           self.imageView.image = image;
            if (self.imageView.image.size.width > [UIScreen mainScreen].bounds.size.width) {
                self.imageView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width * index, [UIScreen mainScreen].bounds.size.height *0.15, [UIScreen mainScreen].bounds.size.width, image.size.height * [UIScreen mainScreen].bounds.size.width /  image.size.width);
            }else
            {
                self.imageView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width * index, [UIScreen mainScreen].bounds.size.height *0.15, [UIScreen mainScreen].bounds.size.width, image.size.height * [UIScreen mainScreen].bounds.size.width /  image.size.width);
                
            }
            
         
            
            if([self.noteArray[index] isEqualToString:@"" ] || self.noteArray[index] == nil )
            {
                self.photoTextView.text = self.imagetitleArray[index];
                
            }else
            {
                self.photoTextView.text = self.noteArray[index];
            }
            
            [self.photoScrollView addSubview:self.imageView];
  
            self.photoNumLable.text = [NSString stringWithFormat:@"%d/%lu",index + 1,(unsigned long)self.noteArray.count ];
            
        }];

}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    if (_delegate && [_delegate respondsToSelector:@selector(picturePhotoViewControllerOpenSwipeGesture)])
     {
         [_delegate picturePhotoViewControllerOpenSwipeGesture];
         
         
     }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//- (NSUInteger)supportedInterfaceOrientations
//{
//    return  UIInterfaceOrientationMaskPortrait;
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
